package cn.hp.customer.controller;

import cn.hp.customer.pojo.TblCoupon;
import cn.hp.customer.pojo.TblCouponSendInfo;
import cn.hp.customer.service.CouponService;
import cn.hp.customer.util.ResponseData;
import cn.hp.customer.util.SysLog;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Controller
@RequestMapping("/coupon")
public class CouponController {

    @Autowired
    private CouponService couponService ;
    // 跳转页面
    @RequestMapping("/couponPage")
    public String couponPage(){
        return "/WEB-INF/back/coupon/couponInfo.html";
    }

    @RequestMapping("/couponSendPage")
    public String couponSendPage(){
        return "/WEB-INF/back/coupon/couponSendInfo.html";
    }

    // 先编写后端，进行接口测试，测试通过根据api中的响应结果进行 设计前端字段
    @ResponseBody
    @RequestMapping("/showCoupon")
    @SysLog(operation = "查询全部优惠券" , method = "coupon_showCoupon" , module = "后端管理")
    public String showCoupon(int page , int limit , @RequestParam( required = true , defaultValue = "") String couponName ) {
        // 创建一个分页对象 ( 当前页数、总页数、总记录数、每页条数、每页数据 )
        ResponseData responseData = couponService.findAllByPage(page, limit, couponName);
        System.out.println(responseData);
        return JSON.toJSONString(responseData);
    }

    @ResponseBody
    @RequestMapping("/addCoupon")
    @SysLog(operation = "添加优惠券" , method = "coupon_addCoupon" , module = "后端管理")
    // springMVC 自动注入参数( 1.简单数据类型 2.Map 3.对象 4.集合(放在对象中) 5.ModelAndView  )  新增核心就是一个表
    public String addCoupon(TblCoupon tblCoupon , HttpServletRequest request){
        // 设置创建者的名称

//        Object adminName = request.getSession().getAttribute("adminName");
//        tblCoupon.setCreateUser(adminName.toString());
        tblCoupon.setCreateUser("zs");
        ResponseData responseData = couponService.addCoupon(tblCoupon);
        return JSON.toJSONString(responseData);
    }

    // 优惠券发送列表展示
    @ResponseBody
    @RequestMapping("/showCouponSend")
    @SysLog(operation = "查询全部优惠券发放" , method = "coupon_showCouponSend" , module = "后端管理")
    public String showCouponSend(int page , int limit ) {
        // 创建一个分页对象 ( 当前页数、总页数、总记录数、每页条数、每页数据 )
        ResponseData responseData = couponService.findAllSendByPage(page, limit);
        System.out.println(responseData);
        return JSON.toJSONString(responseData);
    }

    // 发放优惠券
    // 设计到包含 1.普通的字段上传 2.图片文件的上传 fileupload  3.excel表的上传 poi
    @ResponseBody
    @RequestMapping("/addCouponSend")
    @SysLog(operation = "发放优惠券" , method = "coupon_addCouponSend" , module = "后端管理")
    public String addCouponSend(MultipartFile file , TblCouponSendInfo tblCouponSendInfo , HttpServletRequest request){
        // 获取保存文件路径 按着当前日期进行创建文件路径
        System.out.println(tblCouponSendInfo);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String date = simpleDateFormat.format(new Date());
        String realPath = request.getSession().getServletContext().getRealPath(File.separator + date);
        System.out.println("realPath:" + realPath);
        File filePath = new File(realPath);
        // 如果此目录不存在则创建新目录
        if(!filePath.exists()){
            filePath.mkdirs();
        }
        // 从session域中获取当前登录的用户
        Object adminName = request.getSession().getAttribute("adminName");
        tblCouponSendInfo.setOperatorUser(adminName+"");
        // controller层 只做 请求 响应
        ResponseData responseData = couponService.add(file , tblCouponSendInfo,filePath);
        return JSON.toJSONString(responseData);
    }
}
